<template>
  <div
    class="assemble"
    ref="wrapper"
  >
    <div>
      <list
        @goAssmble="goAssmble(item)"
        v-for="(item,index) in list"
        :key="index"
        :item="item"
      ></list>
    </div>
  </div>
</template>

<script>
import list from '@/components/assemble/assemblelist'
import { timeDiscount } from '@/api/limitedtime'
import BScroll from 'better-scroll'
export default {
  data () {
    return {
      list: [],
      page: 1,
      pageshow: 7
    }
  },
  components: {
    list
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      let data = {
        page: this.page,
        pageshow: this.pageshow
      }
      timeDiscount(data).then((res) => {
        this.$store.commit('updateLoadingStatus', {isLoading: false})
        console.log(this.$store.state.isLoading)
        if (res.data.code === 200) {
          res.data.data.forEach(element => {
            element.money = element.money / 100
            element.preferential_money = element.preferential_money / 100
            element.y_money = element.y_money / 100
          })
          if (this.page === 1) {
            this.list = res.data.data
          } else {
            this.list = [...this.list, ...res.data.data]
          }
          this.page++
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: '暂无更多限时课程'
          })
        }
      })
    },
    // list组件传出跳转
    goAssmble (item) {
      if (window.__wxjs_is_wkwebview !== true) {
        this.$router.push({
          path: '/timelimiteddiscount',
          query: {
            discount_id: item.discount_id,
            curriculum_id: item.curriculum_id
          }
        })
      } else {
        window.location.href = `/timelimiteddiscount?&curriculum_id=${item.curriculum_id}&discount_id=${item.discount_id}`
      }
    },
    initScroll () {
      this.$nextTick(() => {
        let _this = this
        this.scroll = new BScroll(this.$refs.wrapper, {
          click: true,
          scrollY: true,
          tap: true,
          probeType: 3,
          pullUpLoad: {
            threshold: 0
          }
        })
        // 上拉加载
        this.scroll.on('pullingUp', function () {
          _this.getList() // 传一不更值
          _this.scroll.finishPullUp()
          _this.scroll.refresh()
        })
      })
    }
  },
  mounted () {
    this.$nextTick(() => {
      document.title = '限时列表'
    })
    this.initScroll()
  },
  beforeDestroy () {
    document.title = '知运-知识改变命运'
  }
}
</script>
<style lang="less" scoped>
.assemble {
  width: 100%;
  height: 100%;
  background-color: #f8f8f8;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
</style>
